﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using Common;
using Util.PageInfo;

namespace RichECMS.ServerPages.Section
{
    public partial class ArticleSort : Util.BasePage.BasePage
    {
        public static string listValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LitPageName.Text = PageInfo.ShowPagePath(State.intSTID);
                string articlelist = Request["SortID"].Replace("'", "");
                InitDataBind(articlelist);
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void InitDataBind(string articlelist)
        {
            //lst.Items.Add(new ListItem("test", "0"));
            try
            {
                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql("select * from Article where intArticleID in (" + articlelist + ")");
                    if (objArticle != null)
                    {
                        for (int i = 0; i < objArticle.Count; i++)
                        {
                            ListItem Lit = new ListItem();
                            Lit.Text = objArticle.DataItem.chvTitle;
                            Lit.Value = objArticle.DataItem.intArticleID.ToString();
                            lst.Items.Add(Lit);
                            objArticle.SelectRow(i + 1);
                        }
                        lst.ClearSelection();
                    }
                    else
                    {
                        Alert("暂无数据！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 文章向上排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (lst.SelectedIndex > -1)
            {
                string B_Sort = lst.SelectedValue.ToString();

                lst.Items[int.Parse(lst.SelectedIndex.ToString()) - 1].Text = lst.SelectedItem.Text;
                lst.Items[int.Parse(lst.SelectedIndex.ToString()) - 1].Value = lst.SelectedValue.ToString();

                lst.SelectedItem.Text = lst.Items[int.Parse(lst.SelectedIndex.ToString()) - 1].Text;
                lst.SelectedValue = lst.Items[int.Parse(lst.SelectedIndex.ToString()) - 1].Value;

                listValue = lst.Items[int.Parse(lst.SelectedIndex.ToString()) - 1].Value;
            }
        }

        /// <summary>
        /// 提交排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    string[] articlesortlist = Hiddensort.Value.Replace("'", "").Split(',');
                    BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql("select * from Article where intArticleID in (" + Hiddensort.Value.Replace("'", "") + ")");
                    if (objArticle != null)
                    {
                        for (int i = 0; i < objArticle.Count; i++)
                        {
                            for (int j = 0; j < objArticle.Count; j++)
                            {
                                if (objArticle.DataItem.intArticleID.ToString() == articlesortlist[j])
                                {
                                    //objArticle.DataItem.intSort = objArticle.Count - j;
                                    break;
                                }
                            }
                            objArticle.SelectRow(i + 1);
                        }
                        objArticle.Update();

                        SectionsFactory objSFactory = new SectionsFactory();
                        BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(State.intSTID);
                        objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;

                        objSections.Update();
                        objSFactory.Dispose();
                        AlertAndRedirect("排序成功！", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                    }
                    else
                    {
                        AlertAndRedirect("暂无数据！", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 文章向下排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (lst.SelectedIndex > 0)
            {
                string B_Sort = lst.SelectedValue.ToString();

                lst.Items[int.Parse(lst.SelectedIndex.ToString()) + 1].Text = lst.SelectedItem.Text;
                lst.Items[int.Parse(lst.SelectedIndex.ToString()) + 1].Value = lst.SelectedValue.ToString();

                lst.SelectedItem.Text = lst.Items[int.Parse(lst.SelectedIndex.ToString()) + 1].Text;
                lst.SelectedValue = lst.Items[int.Parse(lst.SelectedIndex.ToString()) + 1].Value;

                listValue = lst.Items[int.Parse(lst.SelectedIndex.ToString()) + 1].Value;
            }
        }
    }
}